package edu.hubu.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import edu.hubu.annotations.FilterField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.moon.annotation.FieldIgnore;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.springframework.data.annotation.CreatedDate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author moonlan
 * date 2020/11/13 18:59
 */
@SuppressWarnings(value = "unused")
@Entity
@Table(name = "student")
//逻辑删除注解，删除sql变成了update
@SQLDelete(sql = "update student set student_is_deleted = true where user_sid = ?")
//where条件带上了逻辑删除条件
@Where(clause = "student_is_deleted = false")
@ApiModel("学生类")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
public class Student extends User {
    @Id
    @Column(name = "user_sid")
    @ApiModelProperty(value = "用户工号", position = 1)
    private String userSid;

    @Column(name = "user_name")
            @ApiModelProperty(value = "用户姓名", position = 2)
    protected String userName;

    @Column(name = "user_password")
    @ApiModelProperty(value = "用户密码", position = 3)
    @FieldIgnore
    protected String userPassword;

    @Column(name = "user_telephone_number")
    @ApiModelProperty(value = "用户电话号码", position = 8)
    @FieldIgnore
    protected String userTelephoneNumber;

    @Column(name = "user_effective_date")
    @ApiModelProperty(value = "用户帐号生效时间", position = 9)
    @CreatedDate
    @FieldIgnore
    protected Date userEffectiveDate;

    @Column(name = "user_expiry_date")
    @ApiModelProperty(value = "用户帐号失效时间", position = 10)
    @FieldIgnore
    protected Date userExpiryDate;

    @Column(name = "user_api_key")
    @ApiModelProperty(value = "用户Apikey", position = 11)
    @FieldIgnore
    protected String userApiKey;

    @Column(name = "user_api_key_expiry_date")
    @ApiModelProperty(value = "用户Apikey失效时间", position = 12)
    @FieldIgnore
    protected Date userApiKeyExpiryDate;

    @Column(name = "user_photo_url")
    @ApiModelProperty(value = "用户头像url", position = 13)
    protected String userPhotoUrl;

    @Column(name = "student_is_forbidden")
    @ApiModelProperty(value = "学生是否被禁用", position = 14)
    @FieldIgnore
    private boolean studentIsForbidden = false;

    @Column(name = "student_is_deleted")
    @ApiModelProperty(value = "学生是否被删除", position = 15)
    @FieldIgnore
    private boolean studentIsDeleted = false;

    @Column(name = "user_is_forbidden")
    @ApiModelProperty(value = "用户是否被禁用", position = 14)
    private boolean userIsForbidden = false;

    @Column(name = "user_is_deleted")
    @ApiModelProperty(value = "用户是否被删除", position = 15)
    private boolean userIsDeleted = false;

    @ManyToOne(targetEntity = Role.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "user_role_id", referencedColumnName = "role_id")
    protected Role role;



    @ManyToOne(targetEntity = Institution.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "student_institution_id", referencedColumnName = "institution_id")
    @ApiModelProperty(value = "学生学院", position = 7)
    private Institution institution;

    @ApiModelProperty(value = "学生专业", position = 6)
    @ManyToOne(targetEntity = Major.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "student_major_id", referencedColumnName = "major_id")
    private Major major;

    @ManyToOne(targetEntity = UserClass.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "user_user_class_id", referencedColumnName = "user_class_id")
    @ApiModelProperty(value = "用户班级id", position = 17)
    private UserClass studentClass;

    @Column(name = "student_is_leader")
    @ApiModelProperty(value = "学生是否为班干部", position = 15)
    private Boolean studentIsLeader;


    @OneToOne(mappedBy = "student")
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private Score scores;

    @ManyToMany(targetEntity = StudentCourse.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinTable(name = "student_student_course", joinColumns = {@JoinColumn(name = "student_student_course_student_sid", referencedColumnName = "user_sid")}, inverseJoinColumns = @JoinColumn(name = "student_student_course_course_id", referencedColumnName = "student_course_id"))
    @ApiModelProperty(hidden = true)
    private List<StudentCourse> studentCourses = new ArrayList<>();

    @Override
    public Student setUserSid(String userSid) {
        this.userSid = userSid;
        return this;
    }

    @Override
    public Student setUserName(String userName) {
        this.userName = userName;
        return this;
    }

    @Override
    public Student setUserPassword(String userPassword) {
        this.userPassword = userPassword;
        return this;
    }

    @Override
    public Student setUserTelephoneNumber(String userTelephoneNumber) {
        this.userTelephoneNumber = userTelephoneNumber;
        return this;
    }

    @Override
    public Student setUserEffectiveDate(Date userEffectiveDate) {
        this.userEffectiveDate = userEffectiveDate;
        return this;
    }

    @Override
    public Student setUserExpiryDate(Date userExpiryDate) {
        this.userExpiryDate = userExpiryDate;
        return this;
    }

    @Override
    public Student setUserApiKey(String userApiKey) {
        this.userApiKey = userApiKey;
        return this;
    }

    @Override
    public Student setUserApiKeyExpiryDate(Date userApiKeyExpiryDate) {
        this.userApiKeyExpiryDate = userApiKeyExpiryDate;
        return this;
    }

    @Override
    public Student setUserPhotoUrl(String userPhotoUrl) {
        this.userPhotoUrl = userPhotoUrl;
        return this;
    }

    @Override
    public boolean isUserIsForbidden() {
        return userIsForbidden;
    }

    @Override
    public Student setUserIsForbidden(boolean userIsForbidden) {
        this.userIsForbidden = userIsForbidden;
        return this;
    }

    @Override
    public boolean isUserIsDeleted() {
        return userIsDeleted;
    }

    @Override
    public Student setUserIsDeleted(boolean userIsDeleted) {
        this.userIsDeleted = userIsDeleted;
        return this;
    }

    @Override
    public Role getRole() {
        return role;
    }

    @Override
    public Student setRole(Role role) {
        this.role = role;
        return this;
    }

    public Student(String userSid, String userName, String userPassword, String userTelephoneNumber, Date userEffectiveDate, Date userExpiryDate, String userApiKey, Date userApiKeyExpiryDate, String userPhotoUrl, boolean studentIsForbidden, boolean studentIsDeleted, Institution institution, Major major, UserClass studentClass, Boolean studentIsLeader, Score scores, List<StudentCourse> studentCourses) {
        this.userSid = userSid;
        this.userName = userName;
        this.userPassword = userPassword;
        this.userTelephoneNumber = userTelephoneNumber;
        this.userEffectiveDate = userEffectiveDate;
        this.userExpiryDate = userExpiryDate;
        this.userApiKey = userApiKey;
        this.userApiKeyExpiryDate = userApiKeyExpiryDate;
        this.userPhotoUrl = userPhotoUrl;
        this.studentIsForbidden = studentIsForbidden;
        this.studentIsDeleted = studentIsDeleted;
        this.institution = institution;
        this.major = major;
        this.studentClass = studentClass;
        this.studentIsLeader = studentIsLeader;
        this.scores = scores;
        this.studentCourses = studentCourses;
    }

    public Student() {
    }

    public String getUserSid() {
        return this.userSid;
    }

    public String getUserName() {
        return this.userName;
    }

    public String getUserPassword() {
        return this.userPassword;
    }

    public String getUserTelephoneNumber() {
        return this.userTelephoneNumber;
    }

    public Date getUserEffectiveDate() {
        return this.userEffectiveDate;
    }

    public Date getUserExpiryDate() {
        return this.userExpiryDate;
    }

    public String getUserApiKey() {
        return this.userApiKey;
    }

    public Date getUserApiKeyExpiryDate() {
        return this.userApiKeyExpiryDate;
    }

    public String getUserPhotoUrl() {
        return this.userPhotoUrl;
    }

    public boolean isStudentIsForbidden() {
        return this.studentIsForbidden;
    }

    public boolean isStudentIsDeleted() {
        return this.studentIsDeleted;
    }

    public Institution getInstitution() {
        return this.institution;
    }

    public Major getMajor() {
        return this.major;
    }

    public UserClass getStudentClass() {
        return this.studentClass;
    }

    public Boolean getStudentIsLeader() {
        return this.studentIsLeader;
    }

    public Score getScores() {
        return this.scores;
    }

    public List<StudentCourse> getStudentCourses() {
        return this.studentCourses;
    }

    public void setStudentIsForbidden(boolean studentIsForbidden) {
        this.studentIsForbidden = studentIsForbidden;
    }

    public void setStudentIsDeleted(boolean studentIsDeleted) {
        this.studentIsDeleted = studentIsDeleted;
    }

    public void setInstitution(Institution institution) {
        this.institution = institution;
    }

    public void setMajor(Major major) {
        this.major = major;
    }

    public void setStudentClass(UserClass studentClass) {
        this.studentClass = studentClass;
    }

    public void setStudentIsLeader(Boolean studentIsLeader) {
        this.studentIsLeader = studentIsLeader;
    }

    public void setScores(Score scores) {
        this.scores = scores;
    }

    public void setStudentCourses(List<StudentCourse> studentCourses) {
        this.studentCourses = studentCourses;
    }
}
